# -*- coding: utf-8 -*-
# coding: UTF-8
from scrapy.spiders import CrawlSpider
from scrapy.selector import Selector
from scrapy.http import Request

from ..items import U78zvItem


class u15qlSpider(CrawlSpider):
    name = 'u15qlSpider'
    host = 'http://www.15ql.com/%s'
    start_urls = [
        'p01/index.html',
        'p04/index.html',
    ]

    def start_requests(self):
        for db_type in self.start_urls:
            yield Request(url=self.host % db_type,
                          callback=self.parse_db_key)

    def parse_db_key(self, response):
        sel = Selector(response)
        lists = sel.xpath('//div[@class="typelist"]/ul/li/a')
        for li in lists:
            url = li.xpath('@href').extract()[0]
            yield Request(url=self.host % url, callback=self.parse_ph_info)

        next_page = sel.xpath('//div[@id="page"]/a[last()-1]/@href').extract()[0]
        if next_page:
            yield Request(url=self.host % next_page, callback=self.parse_db_key)

    def parse_ph_info(self, response):
        item = U78zvItem()
        sel = Selector(response)

        name = sel.xpath('//html/head/title/text()').extract()[0]

        imgs = sel.xpath('//div[@id="view1"]/img/@src').extract()

        item['name'] = name.split('-')[0]
        item['image_urls'] = imgs
        yield item
